﻿using ImperialWebSite.Presenters.Status.Classes;
using ImperialWebSite.Presenters.Status.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImperialWebSite.Views.Main.Status
{
    public partial class QueryStatuses : Page, IQueryStatuses
    {
        private QueryStatusesPresenter _presenter;
        private bool _successfulTransaction;

        public object StatusesDataSource
        {
            set { gvStatuses.DataSource = value; }
        }

        public string StatusNameText
        {
            get { return StatusName.Text; }
        }


        public object SessionCurrentStatuses
        {
            get { return Session["currentStatuses"]; }
            set { Session["currentStatuses"] = value; }
        }

        public object SessionCurrentStatus
        {
            get { return Session["currentStatus"]; }
            set { Session["currentStatus"] = value; }
        }


        public string TransactionMessage
        {
            get { return FailureText.Text; }
            set { FailureText.Text = value; }
        }

        public bool SuccessfulTransaction
        {
            get { return _successfulTransaction; }
            set { _successfulTransaction = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new QueryStatusesPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitMessages();
                InitComponents();
            }
        }

        private void InitMessages()
        {
            SuccessfulTransaction = false;
            TransactionMessage = string.Empty;
        }

        private void InitComponents()
        {
            SessionCurrentStatus = null;
            SessionCurrentStatuses = null;
            LoadCurrentStatuses();
            StatusName.Text = string.Empty;
            //btnRegister.Enabled = SessionCurrentStatus != null;
        }

        private void LoadCurrentStatuses()
        {
            _presenter.LoadCurrentStatuses();
            gvStatuses.DataBind();
        }


        protected void GvStatuses_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvStatuses.DataSource = SessionCurrentStatuses;
            gvStatuses.PageIndex = e.NewPageIndex;
            gvStatuses.DataBind();
            SessionCurrentStatus = null;
            StatusName.Text = string.Empty;
        }

        protected void GvStatuses_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = sender as GridView;
            gv.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            SessionCurrentStatus = gv.SelectedDataKey["idStatus"].ToString();
            TransactionMessage = string.Empty;
            StatusName.Text = gv.SelectedRow.Cells[0].Text;
            btnRegister.Enabled = SessionCurrentStatus != null;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (SessionCurrentStatus != null)
            {
                _presenter.UpdateStatus();
                if (SuccessfulTransaction)
                    InitComponents();
            }
            else
                TransactionMessage = "Please select a Status";
        }
    }
}